package com.js.basicutils;

// Created by JS on 2021/1/28.

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class DialogUtil {

    /**
     *  简单的提示框
     * @param context 上下文
     * @param title 标题
     * @param content 内容
     * @param confirmString 确认按钮文字
     * @param listener 监听器
     */
    public static void showTipDialog(Context context, String title, String content, String confirmString, DialogListener listener) {
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        View view = View.inflate(context, R.layout.dialog_tip, null);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvContent = view.findViewById(R.id.tv_content);
        Button btnConfirm = view.findViewById(R.id.btn_confirm);
        Button btnCancel = view.findViewById(R.id.btn_cancel);
        if (title != null) tvTitle.setText(title);
        if (content != null) tvContent.setText(content);
        if (confirmString != null) btnConfirm.setText(confirmString);
        btnConfirm.setOnClickListener(v -> {
            if (listener != null) {
                listener.onConfirm();
            }
            dialog.dismiss();
        });
        btnCancel.setOnClickListener(v -> {
            if (listener != null) {
                listener.onCancel();
            }
            dialog.dismiss();
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setView(view);
        dialog.show();

        //下面这段代码应放在dialog.show()的后面，不然在6.0或者更低版本中报错：
        //requestFeature() must be called before adding content
        Window window = dialog.getWindow();
        // 设置父布局为透明，以显现出自定义布局的圆角
        if (window != null) {
            View decorView = window.getDecorView();
            decorView.setBackgroundColor(Color.TRANSPARENT);
        }
    }

    public interface DialogListener {
        void onConfirm();
        void onCancel();
    }
}
